package com.xx.account.service.impl;

import com.xx.account.domain.AccountBoss;
import com.xx.account.domain.AccountRecord;
import com.xx.account.domain.AccountSendLeaflet;
import com.xx.account.domain.AccountWorker;
import com.xx.account.mapper.AccountBossMapper;
import com.xx.account.mapper.AccountRecordMapper;
import com.xx.account.mapper.AccountSendLeafletMapper;
import com.xx.account.mapper.AccountWorkerMapper;
import com.xx.account.service.IAccountDataStatisticsService;
import com.xx.account.service.IAccountWorkerService;
import com.xx.common.exception.ServiceException;
import com.xx.common.utils.DateUtils;
import com.xx.common.utils.SecurityUtils;
import com.xx.common.utils.StringUtils;
import com.xx.common.utils.uuid.UUID;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 投手Service业务层处理
 *
 * @author ruoyi
 * @date 2024-09-29
 */
@Service
public class AccountStatisticsServiceImpl implements IAccountDataStatisticsService
{
    @Resource
    private AccountRecordMapper accountRecordMapper;

    @Resource
    private AccountBossMapper accountBossMapper;

    @Resource
    private AccountSendLeafletMapper accountSendLeafletMapper;


    @Override
    public Map<String, Object> getBossRecord(AccountRecord accountRecord) {

        Map<String, BigDecimal> result = new LinkedHashMap<>();

        accountRecord.setCreateBy(SecurityUtils.getUserId().toString());

        // 查询当前用户今日接单的老板统计
        List<String> bossIds = accountRecordMapper.selectBossIdsByOrderTimeBetween(accountRecord);
        // 查询得到每个老板id的名字和今日对应的放单数据之和
        for (String bossId : bossIds) {
            accountRecord.setBossId(bossId);
            List<AccountRecord> accountRecords = accountRecordMapper.selectAccountRecordListByBossId(accountRecord);
            BigDecimal priceSum = BigDecimal.ZERO;
            for (AccountRecord record : accountRecords) {
                priceSum = priceSum.add(record.getPrice().multiply(new BigDecimal(record.getCount())));
            }
            // 查询老板名称
            AccountBoss accountBoss = accountBossMapper.selectAccountBossById(bossId);
            result.put(accountBoss.getName(), priceSum);
        }

        // 使用Stream API对Map的value进行倒序排序
        Map<String, Object> sortedMap = result.entrySet()
                .stream()
                .sorted((e1, e2) -> {
                    // 将Object类型的value强制转换为BigDecimal并进行倒序比较
                    BigDecimal value1 = e1.getValue();
                    BigDecimal value2 = e2.getValue();
                    return value2.compareTo(value1); // 倒序比较
                })
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new
                ));

        return sortedMap;
    }

    @Override
    public Map<String, Object> getWorkerRecord(AccountRecord accountRecord) {

        Map<String, Object> result = new HashMap<>();

        accountRecord.setCreateBy(SecurityUtils.getUserId().toString());

        // 查询当前用户今日接的订单
        List<AccountRecord> accountRecords = accountRecordMapper.selectAccountRecordList(accountRecord);
        for (AccountRecord record : accountRecords) {
            Map<String, Object> map = new HashMap<>();
            // 根据订单查询投手分配情况，统计金额
            List<AccountSendLeaflet> accountSendLeaflets = accountSendLeafletMapper.selectAllAccountSendLeafletByRecordId(record.getId());

        }

        result.put("测试1","11");
        result.put("测试2","22");
        result.put("测试3","33");

        return result;
    }
}
